package com.neuedu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.neuedu.entity.MedicalRecord;
import com.neuedu.entity.MedicalRecordDisease;
import com.neuedu.entity.Register;
import com.neuedu.exception.NeueduException;
import com.neuedu.mapper.MedicalRecordMapper;
import com.neuedu.service.DiseaseService;
import com.neuedu.service.MedicalRecordDiseaseService;
import com.neuedu.service.MedicalRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.neuedu.service.RegisterService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 诊断信息表 医生对患者填写的病例 服务实现类
 * </p>
 *
 * @author LiYang
 * @since 2024-11-17
 */
@Service
public class MedicalRecordServiceImpl extends ServiceImpl<MedicalRecordMapper, MedicalRecord> implements MedicalRecordService {
    @Resource
    MedicalRecordDiseaseService medicalRecordDiseaseService;
    @Resource
    DiseaseService diseaseService;
    @Resource
    RegisterService registerService;
    @Override
    @Transactional
    public Boolean add(MedicalRecord medicalRecord, Integer[] diseaseIds) {
        //判断 是否已经存在 该挂号id的诊断记录
        LambdaQueryWrapper<MedicalRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MedicalRecord::getRegisterId, medicalRecord.getRegisterId());
        if (this.getOne(wrapper) != null) {
            throw new NeueduException("该挂号已经存在诊断记录");
        }
        this.save(medicalRecord); //诊断表
        List<MedicalRecordDisease> list = new ArrayList<>();
        for (Integer diseaseId : diseaseIds) {
            list.add(new MedicalRecordDisease(medicalRecord.getId(), diseaseId));
        }
        return medicalRecordDiseaseService.saveBatch(list);
    }
    @Override
    @Transactional
    public Boolean update(MedicalRecord medicalRecord, Integer[] diseaseIds) {
        this.updateById(medicalRecord);
        medicalRecordDiseaseService.delByDedicalId(medicalRecord.getId());
        List<MedicalRecordDisease> list = new ArrayList<>();
        for (Integer diseaseId : diseaseIds) {
            list.add(new MedicalRecordDisease(medicalRecord.getId(), diseaseId));
        }
        return medicalRecordDiseaseService.saveBatch(list);
    }
    @Override
    public MedicalRecord getByRegisterId(Integer registerId) {
        QueryWrapper<MedicalRecord> wrapper = new QueryWrapper<>();
        wrapper.eq("register_id", registerId);
        final MedicalRecord medicalRecord = this.getOne(wrapper);
        final List<Integer> diseaseIds = medicalRecordDiseaseService.getDiseaseIdsByMedicalId(medicalRecord.getId());
        medicalRecord.setDiseaseList(diseaseService.getByIds(diseaseIds));
        return medicalRecord;
    }
    @Override
    @Transactional
    public Boolean quezhen(Integer registerId, String diagnosis, String cure) {
        MedicalRecord medicalRecord = new MedicalRecord();
        medicalRecord.setDiagnosis(diagnosis);
        medicalRecord.setCure(cure);
        UpdateWrapper<MedicalRecord> wrapper = new UpdateWrapper<>();
        wrapper.eq("register_id", registerId);
        registerService.updateById(new Register(registerId, 3));
        return this.update(medicalRecord, wrapper);
    }
}
